﻿Imports TNCDAL
Imports HTLibrary

Public Class ucBuyingRate
    Dim mBR As New MBuyingRate
    Dim mfile As New MFiles
    ' Dim mbook As New MBookings
    Dim mmbl As New MMBL
    Dim uti As New Utilities
    Dim TotalCheckBoxes As Integer = 0
    Dim TotalCheckedCheckBoxes As Integer = 0
    Dim HeaderCheckBox As CheckBox = Nothing
    Dim IsHeaderCheckBoxClicked As Boolean = False
    Dim isDataFile As Boolean = False
    Private currencyVND As tblCurrency = Nothing
    Private currencyForeign As tblCurrency = Nothing
    Dim isNew As Boolean = False
    Dim isEdit As Boolean = False
    Dim reCal As Boolean = False
    Dim isCuoc As Boolean = False
    Dim isExport As Boolean = False 'Hàng nhập
    Dim ctrlHblExchage As New MFileExchange

    'Public Sub showControl(ByVal obj As System.Windows.Forms.Control)
    '    PanelEx1.Controls.Clear()
    '    obj.Dock = DockStyle.Fill
    '    PanelEx1.Controls.Add(obj)
    'End Sub
    Dim _id_file As Integer
    Dim _id_mbl As Integer
    Dim _id_voyage As Integer

    Public Sub New(ByVal id_file As Integer)
        InitializeComponent()
        'DataGridViewHelper.DataGridView_InitEvent(grid)
        DataGridViewHelper.DataGridView_InitEvent(gridEdit)
        'Truyền tham số id_file
        _id_file = id_file
    End Sub
    ''' <summary>
    ''' Lấy tham số truyền vào stoprocedure
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub GetParameter()
        Try
            'Lấy id booking và voyage
            _id_mbl = mmbl.getIDMBL(_id_file)
            _id_voyage = mmbl.getIDVoyage(_id_mbl)

        Catch ex As Exception

        End Try

    End Sub
   
    ''' <summary>
    ''' Kiểm tra xem đã có bảng giá cước của cont hay chưa, nếu chưa có thì không tính lại
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub CheckGiaCuocCont()

        If mBR.ExistCuocCont(_id_file) Then
            isCuoc = True
        Else
            isCuoc = False
        End If

    End Sub
    ''' <summary>
    ''' Kiểm tra Phí của File đã có chưa
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub CheckFeeExist()
        'Kiểm tra nếu mà fee có lưu vào trogn filefee rồi thì load ra để sửa lại

        If mBR.ExistFileFee(_id_file, 1) Then
            'Nếu true tức là sửa
            isNew = False
            isEdit = True
            'btnPaymentVoucher.Enabled = True
        Else
            isNew = True
            isEdit = False
            'btnPaymentVoucher.Enabled = False
        End If

    End Sub
    Private Sub LoadDataToCombo()

        'uti.FillFileCombo(cbFile)
        uti.FillCurrencyCombo(cbCurrency)
        uti.FillCurrencyCombo(EditcbCurrency)
        'uti.FillFeeCombo(cbEditFee, 1)
        isDataFile = True
    End Sub
    Private Sub BindingFeeToGrid()

        If isEdit Then

            Dim query = mBR.getFileFeeList(_id_file)

            gridEdit.DataSource = query
            gridEdit.Visible = True
            grid.Visible = False

            'tự động lưu luôn tỉ giá 
            Dim currentExchanges = ctrlHblExchage.GetFileExchange(_id_file)
            gridExchange.DataSource = currentExchanges
            'xoa het cai cu
            ctrlHblExchage.DeleteAllRecord(_id_file)
            For Each r As DataGridViewRow In gridExchange.Rows
                Dim newEx As New tblFileExchange
                newEx.ID_Currency = CInt(r.Cells("exID_Currency").Value)
                newEx.Ratio = CDbl(r.Cells("exRatio").Value)
                newEx.ID_File = _id_file
                ctrlHblExchage.InsertNewRecord(newEx)
            Next
            'cập nhật lại tỷ giá

        Else
            If reCal = True Then
                'Nếu tính lại rồi thì xóa toàn bộ và lưu lại thông tin mới
                Try
                    mBR.DeleteBuyingFee(_id_file)
                Catch ex As Exception

                End Try

                'Tiến hành lưu
                isNew = True
                reCal = False
            End If
            Dim query
            Try

           

                ' Lấy kết quả từ store procedure
                ' Kiểm tra hàng nhập hay xuất
                ' Nếu là hàng xuất thì tính bình thường
                isExport = mBR.IsExportFile(_id_file)

                If isExport = True Then
                    query = mBR.getBuyingFollowCont(_id_file, _id_mbl, _id_voyage)

                Else
                    'Nếu là hàng nhập thì kiểm tra hàng lẻ

                    If mBR.IsLCLFile(_id_file) = False Then
                        'Nếu đúng là hàng lẻ thì tính theo 
                        query = mBR.getBuyingImportFollowCont(_id_file, _id_mbl, _id_voyage)

                    Else
                        query = mBR.getBuyingFollowCont(_id_file, _id_mbl, _id_voyage)

                    End If
                End If
                grid.Columns(1).ReadOnly = True
                grid.DataSource = query
                gridEdit.Visible = False
                grid.Visible = True
            Catch ex As Exception
                MessageBox.Show(ex.Message)
            End Try

          
            'Chèn mô tả cont vào trogn phần description của grid
            Try
                For index = 0 To grid.RowCount - 1

                    Dim str As String = mBR.DescriptionContOfFile(_id_file, _id_mbl, _id_voyage, grid.Rows(index).Cells(0).Value)

                    'Chèn vào mỗi phần description của phí không phải là phí bill
                    grid.Rows(index).Cells(6).Value = str


                Next
            Catch ex As Exception
                MessageBox.Show("Kiểm tra lại bảng giá cước các container, chưa nhập đủ giá cho các loại cont")
            End Try
            'Sau đó lưu vào 

            'btnEdit.Enabled = False
            'btnSave.Enabled = True
            'Load xong data thì lưu vào CSDL luôn, sau đó hiển thị lại------------------------------
            isNew = True
            Dim objFileFee As tblFileFee
            Dim mby As New MBuyingRate
            For index = 0 To grid.Rows.Count - 1
                objFileFee = New tblFileFee()
                '  Dim chk As DataGridViewCheckBoxCell = CType(grid.Rows(index).Cells("chkCheck"), DataGridViewCheckBoxCell)
                ' If CBool(chk.Value) = True Then
                'objFileFee.Price = CDbl(grid.Rows(index).Cells("Price").Value()) * mby.getNumberofCont(_id_file)
                '  Else
                objFileFee.Price = CDbl(grid.Rows(index).Cells("Price").Value())
                'End If
                Dim cb As DataGridViewComboBoxCell
                cb = grid.Rows(index).Cells("cbCurrency")
                If cb.Value > 0 Then
                    objFileFee.ID_Currency = CInt(grid.Rows(index).Cells("cbCurrency").Value())
                End If
                objFileFee.ID_Fee = CInt(grid.Rows(index).Cells("ID_Fee").Value())
                objFileFee.ID_File = _id_file
                objFileFee.UnitPrice = CDbl(grid.Rows(index).Cells("UnitPrice").Value)
                objFileFee.Quantity = CDbl(grid.Rows(index).Cells("Quantity").Value)
                objFileFee.Description = grid.Rows(index).Cells("Description").Value
                objFileFee.InvoiceNo = grid.Rows(index).Cells("invoiceNo").Value()

                If objFileFee.Price > 0 Then
                    mby.InsertNewRecord(objFileFee)
                End If

                ' btnSave.Enabled = False
            Next
            isNew = False
            isEdit = True
            '----------------------------------------------------------
            BindingFeeToGrid()
        End If


        '  grid.Columns(0).Visible = False
        gridEdit.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells
        gridEdit.Columns(5).DefaultCellStyle.WrapMode = DataGridViewTriState.True
        grid.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells




        grid.ReadOnly = True
        gridEdit.ReadOnly = True
        gridEdit.EditMode = DataGridViewEditMode.EditOnEnter
        grid.EditMode = DataGridViewEditMode.EditOnEnter
        TotalCheckBoxes = grid.RowCount
        TotalCheckedCheckBoxes = 0

        '---------------------THANH THÊM PHẦN TÍNH TỔNG NGÀY 24/10/2013-------------------
        CalculateTotalBuying()
        '--------------------HẾT PHẦN THANH THÊM------------------------------------------
    End Sub
    Private Sub AddHeaderCheckbox()
        HeaderCheckBox = New CheckBox()
        HeaderCheckBox.Size = New Size(15, 15)
        Me.grid.Controls.Add(HeaderCheckBox)
    End Sub
   
    Private Sub ucBuyingRate_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        

        GetParameter()
        LoadDataToCombo()
        CheckGiaCuocCont()
        'Sau khi kiểm tra giá cước, nếu mà có giá cước rồi thì mới chạy các bước tiếp theo không thì quay lại

        If isCuoc = False Then
            MessageBox.Show("Chưa có bảng giá cước")
            Return
            'return
        End If

        CheckFeeExist()
        BindingFeeToGrid()

        Formatgrid()

    End Sub

    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click

        'If reCal = True Then
        '    'Nếu tính lại rồi thì xóa toàn bộ và lưu lại thông tin mới

        '    mBR.DeleteBuyingFee(_id_file)
        '    'Tiến hành lưu
        '    isNew = True
        '    reCal = False
        'End If

        'If isNew Then
        '    Dim objFileFee As tblFileFee
        '    Dim mby As New MBuyingRate
        '    For index = 0 To grid.Rows.Count - 1
        '        objFileFee = New tblFileFee()
        '        '  Dim chk As DataGridViewCheckBoxCell = CType(grid.Rows(index).Cells("chkCheck"), DataGridViewCheckBoxCell)
        '        ' If CBool(chk.Value) = True Then
        '        'objFileFee.Price = CDbl(grid.Rows(index).Cells("Price").Value()) * mby.getNumberofCont(_id_file)
        '        '  Else
        '        objFileFee.Price = CDbl(grid.Rows(index).Cells("Price").Value())
        '        'End If
        '        Dim cb As DataGridViewComboBoxCell
        '        cb = grid.Rows(index).Cells("cbCurrency")
        '        If cb.Value > 0 Then
        '            objFileFee.ID_Currency = CInt(grid.Rows(index).Cells("cbCurrency").Value())
        '        End If
        '        objFileFee.ID_Fee = CInt(grid.Rows(index).Cells("ID_Fee").Value())
        '        objFileFee.ID_File = _id_file
        '        objFileFee.UnitPrice = CInt(grid.Rows(index).Cells("UnitPrice").Value)
        '        objFileFee.Quantity = CDbl(grid.Rows(index).Cells("Quantity").Value)
        '        objFileFee.Description = grid.Rows(index).Cells("Description").Value
        '        objFileFee.InvoiceNo = grid.Rows(index).Cells("invoiceNo").Value()
        '        mby.InsertNewRecord(objFileFee)
        '        ' btnSave.Enabled = False
        '    Next
        '    isNew = False
        '    isEdit = True
        'ElseIf isEdit Then
        'Lưu giá trị tiền
        'Lưu thông tin giá vào file--------------------------Sửa theo dữ liệu mới, cso nhiều loại tiền
        'Dim obj As New tblFile
        'obj.ID_File = _id_file

        'If txtRatio.Text <> "" Then
        '    obj.Ratio = Double.Parse(txtRatio.Text.Replace(",", ""))
        'Else
        '    obj.Ratio = 0
        'End If
        'mfile.UpdateRatioBuying(obj)
        '---------------------------------
        Dim objFileFee As tblFileFee
        Dim mby As New MBuyingRate
        For index = 0 To gridEdit.Rows.Count - 1
            objFileFee = New tblFileFee()
            'Dim chk As DataGridViewCheckBoxCell = CType(grid.Rows(index).Cells("chkCheck"), DataGridViewCheckBoxCell)
            'If CBool(chk.Value) = True Then
            '    objFileFee.Price = CDbl(grid.Rows(index).Cells("Price").Value()) * mby.getNumberofCont(_id_file)
            'Else
            objFileFee.Price = CDbl(gridEdit.Rows(index).Cells("EditPrice").Value())
            'End If

            objFileFee.ID_FileFee = CInt(gridEdit.Rows(index).Cells("ID_FileFee").Value())

            objFileFee.ID_File = _id_file

            Dim cb As DataGridViewComboBoxCell

            cb = gridEdit.Rows(index).Cells("EditcbCurrency")
            If cb.Value > 0 Then
                objFileFee.ID_Currency = CInt(gridEdit.Rows(index).Cells("EditcbCurrency").Value())
                'Kiểm tra xem bảng Tiền tệ của File có loại tiền này chưa, chưa có thì thêm vào
                Dim mfe As New MFileExchange

                If mfe.IsCurrencyExist(_id_file, cb.Value) = True Then
                    ' true là có rồi thì thôi
                Else
                    'Chưa có thì thêm vào
                    Dim obj As New tblFileExchange
                    obj.ID_File = _id_file
                    obj.ID_Currency = cb.Value
                    obj.Ratio = 0
                    mfe.InsertNewRecord(obj)
                End If

            End If
            'If cb.Value > 0 Then
            objFileFee.ID_Fee = CInt(gridEdit.Rows(index).Cells("EditIDFee").Value())
            'End If
            '    objFileFee.IsBuying = True
            objFileFee.UnitPrice = CDbl(gridEdit.Rows(index).Cells("EditUnitPrice").Value)
            objFileFee.Quantity = CDbl(gridEdit.Rows(index).Cells("EditQuantity").Value)
            objFileFee.Description = gridEdit.Rows(index).Cells("EditDescription").Value
            objFileFee.InvoiceNo = gridEdit.Rows(index).Cells("EditInvoiceNo").Value()
            mby.UpdateRecord(objFileFee)
            ' btnSave.Enabled = False
        Next
        'End If

        btnEdit.Enabled = True
        btnSave.Enabled = False
        grid.ReadOnly = True
        gridEdit.ReadOnly = True
        btnCancel.Enabled = False
        btnReCalculate.Enabled = True
        'txtRatio.Enabled = False
        BindingFeeToGrid()
        CalculateTotalBuying()
    End Sub

    

  
    Public Sub RefreshData()
        If isDataFile Then

            '_id_file = cbFile.SelectedValue
            CheckFeeExist()
            GetParameter()
            BindingFeeToGrid()
        End If
    End Sub
   
    Private Sub btnEdit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEdit.Click
        grid.ReadOnly = False
        gridEdit.ReadOnly = False
        btnEdit.Enabled = False
        btnSave.Enabled = True
        btnCancel.Enabled = True
        btnReCalculate.Enabled = True
        If isEdit Then
            gridEdit.Focus()
        Else
            grid.Focus()
        End If

    End Sub

    
   
#Region "Thanh - cập nhật 24/10/2013"
     

    Private Sub CalculateTotalBuying()
        Dim gridTinhTong As DevComponents.DotNetBar.Controls.DataGridViewX
        Dim cln As String = ""
        Dim clnC As String = ""
        If isNew Then
            gridTinhTong = grid
            cln = "Price"
            clnC = "cbCurrency"
        Else
            gridTinhTong = gridEdit
            cln = "EditPrice"
            clnC = "EditcbCurrency"
        End If
        Dim total As Double = 0
        Dim ratio As Double = 1
        Dim mfe As New MFileExchange

        'If txtRatio.Text <> "" Then
        '    ratio = CDbl(txtRatio.Text.Replace(".", ""))
        'Else
        '    ratio = 1
        'End If
        For i As Integer = 0 To gridTinhTong.Rows.Count - 1
            Try
                Dim priceCurrency As Integer = gridTinhTong.Rows(i).Cells(clnC).Value.ToString
                Dim price As Double = CDbl(gridTinhTong.Rows(i).Cells(cln).Value)
                ratio = mfe.GetRatioCurrency(_id_file, priceCurrency)
                If ratio = 0 Then
                    ratio = 1
                End If

                If (priceCurrency <> 1) Then

                    If priceCurrency = 1 Then
                        'Tiền Việt tính ngược lại
                        price = price / ratio
                    Else
                        price = price * ratio
                    End If
                Else
                    'Nếu 2 loại tiền giống nhau thì chỉ cộng vào

                End If
                total += price
                '----------------
                'Dim price As Double

                'price = Double.Parse(gridTinhTong.Rows(i).Cells(cln).Value.ToString)
                'total += price
            Catch ex As Exception

            End Try
        Next
        lblTotal.Text = total.ToString("#,##0.##")
    End Sub
#End Region
    
    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        CheckFeeExist()
        BindingFeeToGrid()
        btnCancel.Enabled = False
        btnEdit.Enabled = True
        btnSave.Enabled = False
        btnReCalculate.Enabled = True
        'txtRatio.Enabled = False
        reCal = False
    End Sub

    Private Sub btnReCalculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnReCalculate.Click
        'Nếu bấm nút tính lại, tức là tính theo giá trị cũ
        reCal = True
        isEdit = False
        isNew = True
        'btnEdit.Enabled = False
        'btnSave.Enabled = True
        'btnCancel.Enabled = True
        'btnReCalculate.Enabled = False
        CheckGiaCuocCont()

        If isCuoc = True Then
           
        Else
            reCal = False
            isEdit = False
            isNew = False
            MessageBox.Show("Xem lại giá cước của cont")
            Return
        End If
        BindingFeeToGrid()
        Formatgrid()
     
    End Sub

    Private Sub gridEdit_CellEndEdit(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles gridEdit.CellEndEdit
       
        Select Case e.ColumnIndex
            Case 3, 4
                'tự động cập nhật lại giá trị của price

                Dim clnunit As String = ""
                Dim clnquan As String = ""
                Dim clnpri As String = ""
                'If isNew Then
                '    gridTinhTong = grid
                '    clnunit = "UnitPrice"
                '    clnquan = "Quantity"
                '    clnpri = "Price"
                'Else
                'gridTinhTong = gridEdit
                clnunit = "EditUnitPrice"
                clnquan = "EditQuantity"
                clnpri = "EditPrice"
                'End If
                Try
                    Dim UnitPrice As Double = Double.Parse(gridEdit.Rows(e.RowIndex).Cells(clnunit).Value.ToString)
                    Dim Quantity As Double = Double.Parse(gridEdit.Rows(e.RowIndex).Cells(clnquan).Value.ToString)
                    Dim total As Double = UnitPrice * Quantity
                    gridEdit.Rows(e.RowIndex).Cells(clnpri).Value = total
                Catch ex As Exception
                    gridEdit.Rows(e.RowIndex).Cells(clnpri).Value = ""
                End Try
        End Select
        If (e.ColumnIndex = 5) Then
            'cập nhật lại tổng
            CalculateTotalBuying()
        End If
         
    End Sub

    Private Sub grid_CellEndEdit(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles grid.CellEndEdit
        If (e.ColumnIndex = 4) Then
            'cập nhật lại tổng
            CalculateTotalBuying()
        End If
        Select Case e.ColumnIndex
            Case 3
                'tự động cập nhật lại giá trị của price

                Dim clnunit As String = ""
                Dim clnquan As String = ""
                Dim clnpri As String = ""
                'If isNew Then
                '    gridTinhTong = grid
                '    clnunit = "UnitPrice"
                '    clnquan = "Quantity"
                '    clnpri = "Price"
                'Else
                'gridTinhTong = gridEdit
                clnunit = "UnitPrice"
                clnquan = "Quantity"
                clnpri = "Price"
                'End If
                Try
                    Dim UnitPrice As Double = Double.Parse(grid.Rows(e.RowIndex).Cells(clnunit).Value.ToString)
                    Dim Quantity As Double = Double.Parse(grid.Rows(e.RowIndex).Cells(clnquan).Value.ToString)
                    Dim total As Double = UnitPrice * Quantity
                    grid.Rows(e.RowIndex).Cells(clnpri).Value = total
                Catch ex As Exception
                    grid.Rows(e.RowIndex).Cells(clnpri).Value = ""
                End Try
        End Select

    End Sub

     
    Private Sub grid_CellBeginEdit(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellCancelEventArgs) Handles grid.CellBeginEdit
        If (e.ColumnIndex = 1 Or e.ColumnIndex = 2 Or e.ColumnIndex = 4 Or e.ColumnIndex = 7) AndAlso e.RowIndex < grid.RowCount Then e.Cancel = True
    End Sub

    Private Sub gridEdit_CellBeginEdit(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellCancelEventArgs) Handles gridEdit.CellBeginEdit
        If (e.ColumnIndex = 2 Or e.ColumnIndex = 5) AndAlso e.RowIndex < gridEdit.RowCount Then
            e.Cancel = True
        Else
            CalculateTotalBuying()
        End If


    End Sub

    Private Sub cboCurrency_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)

        CalculateTotalBuying()
    End Sub
    Private Sub Formatgrid()

        grid.Columns(2).DefaultCellStyle.Format = "#,##0.##"
        grid.Columns(2).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight

        grid.Columns(3).DefaultCellStyle.Format = "#,##0.##"
        grid.Columns(3).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
        grid.Columns(4).DefaultCellStyle.Format = "#,##0.##"
        grid.Columns(4).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight

        gridEdit.Columns(3).DefaultCellStyle.Format = "#,##0.##"
        gridEdit.Columns(3).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
        gridEdit.Columns(5).DefaultCellStyle.Format = "#,##0.##"
        gridEdit.Columns(5).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
        gridEdit.Columns(4).DefaultCellStyle.Format = "#,##0.##"
        gridEdit.Columns(4).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight


    End Sub

  


   

    Private Sub btExchange_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btExchange.Click
        If (panelExchange.Visible = True) Then
            panelExchange.Visible = False
        Else
            panelExchange.Visible = True
        End If
    End Sub

    Private Sub panelExchange_VisibleChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles panelExchange.VisibleChanged
        If (panelExchange.Visible = True) Then
            'load thông tin exchange
            Dim currentExchanges = ctrlHblExchage.GetFileExchange(_id_file)
            gridExchange.DataSource = currentExchanges
            EnableButtonsEx(True)
        End If
    End Sub

    Private Sub ButtonX1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEditEx.Click
        EnableButtonsEx(False)
    End Sub
    Private Sub EnableButtonsEx(ByVal val As Boolean)
        gridExchange.ReadOnly = val
        gridExchange.Columns("exCurr").ReadOnly = True
        Try

            gridExchange.CurrentCell = gridExchange.CurrentRow.Cells("exRatio")
        Catch ex As Exception

        End Try
        'gridExchange.CurrentCell = gridExchange.CurrentRow.Cells("exRatio")
        btnEditEx.Enabled = val
        btnUpdateEx.Enabled = Not val
        btnCancelEx.Enabled = Not val
    End Sub

    Private Sub btnUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpdateEx.Click
        'xoa het cai cu
        ctrlHblExchage.DeleteAllRecord(_id_file)
        For Each r As DataGridViewRow In gridExchange.Rows
            Dim newEx As New tblFileExchange
            newEx.ID_Currency = CInt(r.Cells("exID_Currency").Value)
            newEx.Ratio = CDbl(r.Cells("exRatio").Value)
            newEx.ID_File = _id_file
            ctrlHblExchage.InsertNewRecord(newEx)
        Next
        'cập nhật lại tỷ giá
        CalculateTotalBuying()
        panelExchange.Visible = False
    End Sub

    Private Sub ButtonX2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancelEx.Click
        panelExchange.Visible = False
    End Sub

    Private Sub btExClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btExClose.Click
        panelExchange.Visible = False
    End Sub

    Private Sub gridEdit_EditingControlShowing(sender As System.Object, e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles gridEdit.EditingControlShowing
        '    RaiseEvent  e.Control.KeyPress -= New KeyPressEventHandler(AddressOf Column1_KeyPress)
        If gridEdit.CurrentCell.ColumnIndex = 3 Then
            'Desired Column
            Dim tb As TextBox = TryCast(e.Control, TextBox)
            If tb IsNot Nothing Then
                AddHandler tb.KeyPress, AddressOf Column1_KeyPress
                'tb.KeyPress += New KeyPressEventHandler(AddressOf Column1_KeyPress)
            End If
        End If
    End Sub
    Private Sub Column1_KeyPress(sender As Object, e As KeyPressEventArgs)
        If Not Char.IsControl(e.KeyChar) AndAlso Not Char.IsDigit(e.KeyChar) Then
            e.Handled = True
        End If
    End Sub

    Private Sub gridExchange_EditingControlShowing(sender As System.Object, e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles gridExchange.EditingControlShowing
        '    RaiseEvent  e.Control.KeyPress -= New KeyPressEventHandler(AddressOf Column1_KeyPress)
        If gridExchange.CurrentCell.ColumnIndex = 2 Then
            'Desired Column
            Dim tb As TextBox = TryCast(e.Control, TextBox)
            If tb IsNot Nothing Then
                AddHandler tb.KeyPress, AddressOf Column1_KeyPress
                'tb.KeyPress += New KeyPressEventHandler(AddressOf Column1_KeyPress)
            End If
        End If
    End Sub
End Class
